package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findById(Integer memberId) {
        return memberDao.selectById(memberId);
    }

    @Override
    public Member isCreate(Member member) {
        log.info("[用户信息]member:{}", member);
        Member dbMember = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if (null != dbMember) {
            //会员已经存在
            return dbMember;
        } else {
            //新增会员数据
            member.setRegTime(new Date());
            memberDao.insert(member);
        }
        return memberDao.selectById(member.getId());
    }

    @Override
    public void addMember(Member member) {
        Member dbMember = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if (dbMember == null) {
            member.setRegTime(new Date());
            memberDao.insertMember(member);
        }

    }

    @Override
    public Member findByPhoneNumber(String telephone) {
        return memberDao.selectByPhoneNumber(telephone);
    }

    @Override
    public void updateById(Member member) {
        memberDao.updateById(member);
    }
}
